// import store from '../store/index.js';	//vuex
const url_all = { // Request baseUrl
	'DEV':'http://221.237.108.156:8866/apiTest/',
	'BUILD':'http://221.237.108.156:8866/apiTest/'
	// 'DEV':'http://221.237.108.156:8866/api/',
	// 'BUILD':'http://221.237.108.156:8866/api/'
	}
let server_url = '';
let token = '';

process.env.NODE_ENV === 'development' ? server_url = url_all['DEV'] : server_url = url_all['BUILD']; // 根据不同的环境配置不同的地址
uni.setStorage({ // 缓存一个全局路径
	key: 'BaseUrl',
	data: server_url,
	success() {
		console.log('缓存成功')
	},   
	fail() {
		console.log('缓存失败')
   }
})
function service(options = {}) {
	let access_Token = uni.getStorageSync('uerInfo') // 获取token
	token = access_Token.access_token ? `Bearer ${access_Token.access_token}` : 'Basic aGpfYXBwOmhqX2FwcA=='
	// Default configuration
	options.url = `${server_url}${options.url}`;
	
	options.data = options.params;
	
	options.header = { // 将token组装到请求头内
		// 'content-type': 'application/x-www-form-urlencoded',	
		'Authorization': `${token}`
	}
	// 开始请求 options 是组装好的请求参数结构
	return uni.request(options).then(res => {
			if (res[1].data) {
				return res[1];
			} else {
				throw res[1]
			}
		}).catch(parmas => {
			switch (parmas.errorcode) {
				case 401:
					uni.clearStorageSync();
					break;
				default:
					uni.showToast({
						title: parmas.msg,
						icon: "none"
					});
					return Promise.reject();
			}
		})
}
export default service;
